package com.example.demo.service.imply;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.QuestionLabelMapper;
import com.example.demo.mapper.SubjectMapper;
import com.example.demo.pojo.TreeQuestionLabel;
import com.example.demo.pojo.entity.QuestionLabel;
import com.example.demo.pojo.entity.Subject;
import com.example.demo.pojo.entity.SubjectFile;
import com.example.demo.pojo.vo.LabelShowVO;
import com.example.demo.service.ILabelService;
import com.example.demo.utils.UnitTreeSolveData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author fjh
 */
@Service
@RequiredArgsConstructor
public class ILabelServiceImply extends ServiceImpl<QuestionLabelMapper, QuestionLabel> implements ILabelService {
    private final QuestionLabelMapper questionLabelMapper;
    private final SubjectMapper subjectMapper;
    //辅助函数-构建所需要树枝
    TreeQuestionLabel initTreeQuestionLabel(QuestionLabel nodeQuestionLabel,List<Integer>needIds){
        if(nodeQuestionLabel==null){
            return null;
        }
        TreeQuestionLabel tempTreeQuestionLabel=new TreeQuestionLabel();
        tempTreeQuestionLabel.initQuestion(nodeQuestionLabel);
        //查找这个节点的所有子节点
        String childNodeId=nodeQuestionLabel.getNodeId();
        QueryWrapper<QuestionLabel>queryWrapper=new QueryWrapper<>();
        queryWrapper.likeRight("node_id",childNodeId);
        queryWrapper.eq("level",nodeQuestionLabel.getLevel()+1);
        List<QuestionLabel>childrenList=questionLabelMapper.selectList(queryWrapper);
        for(int i=0;i<childrenList.size();i++){
            if(childrenList.get(i).getLevel()==nodeQuestionLabel.getLevel()+1&&needIds.contains(childrenList.get(i).getId())){
                TreeQuestionLabel tempTree=initTreeQuestionLabel(childrenList.get(i),needIds);
                if(tempTree!=null){
                    tempTreeQuestionLabel.addChild(tempTree);
                }
            }
        }
        return tempTreeQuestionLabel;
    }
    //辅助函数--递归构建labelVO
    List<LabelShowVO>solveLabelShowVO(List<TreeQuestionLabel>children){
        //递归终止条件
        if(children==null){
            return null;
        }
        List<LabelShowVO>result = new ArrayList<>();
        for(int i=0;i<children.size();i++){
            TreeQuestionLabel treeQuestionLabel=children.get(i);
            QuestionLabel questionLabel=treeQuestionLabel.getQuestionLabel();
            LabelShowVO labelShowVO=new LabelShowVO();
            labelShowVO.initLabel(questionLabel);
            List<LabelShowVO>solveResult=solveLabelShowVO(treeQuestionLabel.getChildren());
            labelShowVO.setQuestionLabels(solveResult);
            result.add(labelShowVO);
        }
        return result;
    }
    //打包函数--将树的信息打包成labelShowVO进行返回
    List<LabelShowVO> completeLabelVOList(QuestionLabel rootLabel,List<Integer>needIdList){
        List<LabelShowVO>result=new ArrayList<>();
        TreeQuestionLabel treeQuestionLabel=initTreeQuestionLabel(rootLabel,needIdList) ;
        LabelShowVO labelShow=new LabelShowVO();
        labelShow.initLabel(rootLabel);
        List<LabelShowVO>solveResult=solveLabelShowVO(treeQuestionLabel.getChildren());
        labelShow.setQuestionLabels(solveResult);
        for(int i=0;i<solveResult.size();i++){
            result.add(solveResult.get(i));
        }
        return  result;
    }
    @Override
    public List<LabelShowVO> initLabel(int rootId)  {
        Subject subject=subjectMapper.selectById(rootId);
        int labelId=subject.getLabelId();
        QuestionLabel rootLabel=questionLabelMapper.selectById(labelId);
        QueryWrapper<QuestionLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id"); // 指定只查询 id 字段
        List<Integer>needIdList=questionLabelMapper.selectList(queryWrapper).stream().map(QuestionLabel::getId).collect(Collectors.toList());
        return completeLabelVOList(rootLabel,needIdList);
    }

    @Override
    public QuestionLabel searchLabelById(int labelId) {
        return questionLabelMapper.searchById(labelId);
    }

    @Override
    public List<LabelShowVO> searchLabelByName(int rootId,String labelName){
        Subject subject=subjectMapper.selectById(rootId);
        int labelId=subject.getLabelId();
        List<LabelShowVO>result = new ArrayList<>();
        //获得满足模糊条件的label
        QueryWrapper<QuestionLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", labelName);
        queryWrapper.likeRight("node_id",labelId+"-");
        List<QuestionLabel>questionLabelList=questionLabelMapper.selectList(queryWrapper);
        System.out.println("查找到的ql"+questionLabelList);
        //构建label树
        List<Integer>needIdList=new ArrayList<>();
        for(QuestionLabel questionLabel : questionLabelList){
            String[] ids=questionLabel.getNodeId().split("-");
            for(String id : ids){
                if(!needIdList.contains(id)){
                    needIdList.add(Integer.valueOf(id));
                }
            }
        }
        System.out.println("需要的id"+needIdList);
        QuestionLabel rootLabel=questionLabelMapper.selectById(rootId);
        result=completeLabelVOList(rootLabel,needIdList);
        return result;
    }

    @Override
    public QuestionLabel addLabelBySameLevel(int labelId, String labelName) {
        QuestionLabel questionLabel=questionLabelMapper.selectById(labelId);
        int level=questionLabel.getLevel();
        String nodeId=questionLabel.getNodeId();
        int parentId=questionLabel.getParentId();
        UnitTreeSolveData unitLabel=new UnitTreeSolveData();
        QuestionLabel newQuestionLabel=new QuestionLabel();
        newQuestionLabel.setName(labelName);
        questionLabelMapper.insert(newQuestionLabel);
        int autoId=newQuestionLabel.getId();
        String parentNodeId="";
        //同级为根目录时会出现歧义
        if(parentId!=0){
            newQuestionLabel.setParentId(parentId);
            parentNodeId=questionLabelMapper.selectById(parentId).getNodeId();
            newQuestionLabel.setLevel(level);
        }else{
            newQuestionLabel.setParentId(labelId);
            parentNodeId=nodeId;
            newQuestionLabel.setLevel(level+1);
        }
        String newNodeId=parentNodeId+autoId+"-";
        newQuestionLabel.setNodeId(newNodeId);
        UpdateWrapper<QuestionLabel> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",autoId);
        //实现更新
        questionLabelMapper.update(newQuestionLabel,updateWrapper);
        return questionLabel;
    }

    @Override
    public QuestionLabel addLabelByChildrenLevel(int labelId, String labelName) {
        QuestionLabel questionLabel=questionLabelMapper.selectById(labelId);
        int level=questionLabel.getLevel();
        int parentId=questionLabel.getParentId();
        QuestionLabel newQuestionLabel=new QuestionLabel();
        newQuestionLabel.setLevel(level+1);
        newQuestionLabel.setName(labelName);
        newQuestionLabel.setParentId(questionLabel.getId());
        questionLabelMapper.insert(newQuestionLabel);
        int autoId=newQuestionLabel.getId();
        String newNodeId=questionLabel.getNodeId()+autoId+"-";
        newQuestionLabel.setNodeId(newNodeId);
        UpdateWrapper<QuestionLabel> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",autoId);
        //实现更新
        questionLabelMapper.update(newQuestionLabel,updateWrapper);
        return questionLabel;
    }

    @Override
    public List<Integer> getLabelId(int subjectId) {
        Subject subject=subjectMapper.selectById(subjectId);
        int labelId=subject.getLabelId();
        String nodeId=labelId+"-";
        QueryWrapper<QuestionLabel>queryLabelWrapper=new QueryWrapper<>();
        queryLabelWrapper.likeRight("node_id",nodeId);
        return questionLabelMapper.selectList(queryLabelWrapper).stream().map(QuestionLabel::getId).collect(Collectors.toList());
    }
}
